package org.mspring.mlog.service.impl;

import java.util.Date;

import org.mspring.mlog.entity.JobLog;
import org.mspring.mlog.service.JobLogService;
import org.mspring.platform.dao.hibernate.GenericDao;
import org.mspring.platform.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author Gao Youbo
 * @since 2013-06-18 16:34:42
 */
@Service
@Transactional
public class JobLogServiceImpl extends GenericDao<JobLog, java.lang.Long> implements JobLogService {

    @Override
    public JobLog save(JobLog object) {
        // TODO Auto-generated method stub
        return super.save(object);
    }

    @Override
    public JobLog getLastestExecute(Long jobId) {
        // TODO Auto-generated method stub
        return uniqueResult("select jobLog from JobLog jobLog where jobLog.job.id = ? and jobLog.success = true order by jobLog.time desc", jobId);
    }

    @Override
    public void removeJobLog(int days) {
        // TODO Auto-generated method stub
        String hql = "delete JobLog jobLog ";
        if (days > 0) {
            Date date = DateUtils.addDays(new Date(), -days);
            String dateStr = DateUtils.format(date, DateUtils.YYYY_MM_DD_HH_MM_SS);
            hql += "where jobLog.time < '" + dateStr + "'";
        }
        executeUpdate(hql);
    }

}
